package com.qianfeng.zhangwei.touchevent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;

import com.qianfeng.zhangwei.touchevent.adapter.MainPageAdapter;
import com.qianfeng.zhangwei.touchevent.fragment.NewFragment;

import java.util.ArrayList;

/**
 *
 */

/**
 * ViewPager + ViewPager
 *
 * ViewPager+ acitionbar
 * ViewPagerIndctor
 */

public class MainActivity extends AppCompatActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private static final String TAG = "MainActivity";
    private ActionBar actionBar;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        initViewPager();
        initActionBar();
    }

    private void initViewPager() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new NewFragment());
        fragments.add(new NewFragment());
        fragments.add(new NewFragment());
        fragments.add(new NewFragment());
        fragments.add(new NewFragment());
        fragments.add(new NewFragment());
        fragments.add(new NewFragment());
        viewPager = (ViewPager) this.findViewById(R.id.main_viewpager);
        MainPageAdapter adapter = new MainPageAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
//        viewPager.setOffscreenPageLimit(1);
        viewPager.setOnPageChangeListener(this);
    }

    private void initActionBar() {
        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("今日新品").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("特卖").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("昨日上线").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("最后疯狂").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("最后疯狂").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("最后疯狂").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("最后疯狂").setTabListener(this));
    }


    /**
     * 事件分发
     * 以隧道形式,从根元素依次向下传递
     * Activity
     * 1> 如果返回 true 表示事件不向下分发
     * 2> 如果返回  false 表示事件不向下分发
     * 3> 如果返回系统默认  事件才会向下传递
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.e(TAG, "dispatchTouchEvent");
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 事件处理
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
